library(foreign)
setwd("~/Replication Code/")

#---Loading in data ----
anes9297 <- read.dta("./data/anes9297_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

#-ANES 9294-----
anes9294.ps <- na.omit(anes9297[c("white_int92", "white_int94",
                                   "pid92_7scaled", "pid94_7scaled", 
                                   "ftdif_wb92_sc", "ftdif_wb94_sc", "V940005",
                                   "ps_sc_92")])

# Median split for PK
anes9294.ps$pk_hi_atabvMed <- ifelse(anes9294.ps$ps_sc_92 >= median(anes9294.ps$ps_sc_92),
                                     1, 0)

# Low PK
### PID on Affect
m1.ps.anes.lo <- lm(ftdif_wb94_sc ~  pid92_7scaled  + ftdif_wb92_sc, 
                     data = anes9294.ps, weights = V940005, 
                     subset = (white_int92 == white_int94) & pk_hi_atabvMed == 0)
summary(m1.ps.anes.lo)

### Affect on PID
m10.ps.anes.lo <- lm(pid94_7scaled ~  pid92_7scaled  + ftdif_wb92_sc, 
                      data = anes9294.ps, weights = V940005, 
                      subset = (white_int92 == white_int94) & pk_hi_atabvMed == 0)
summary(m10.ps.anes.lo)


# High PK
### PID on Affect
m1.ps.anes.hi <- lm(ftdif_wb94_sc ~  pid92_7scaled  + ftdif_wb92_sc, 
                     data = anes9294.ps, weights = V940005, 
                     subset = (white_int92 == white_int94) & pk_hi_atabvMed == 1)
summary(m1.ps.anes.hi)

### Affect on PID
m10.ps.anes.hi <- lm(pid94_7scaled ~  pid92_7scaled  + ftdif_wb92_sc, 
                      data = anes9294.ps, weights = V940005, 
                      subset = (white_int92 == white_int94) & pk_hi_atabvMed == 1)
summary(m10.ps.anes.hi)



### VSG -----
vsg.ps <- na.omit(vsg.dat[c("pk_12", "pid7_sc_12", "pid7_sc_16", "weight",
                            "nwa_sc_12", "nwa_sc_16")])

# Code high PK folks including median with high
vsg.ps$pk_hi_atabvMed <- ifelse(vsg.ps$pk_12 >= median(vsg.ps$pk_12),
                                1, 0)

### Low PK
# PID on Affect
m1.ps.vsg.lo <- lm(nwa_sc_16 ~ pid7_sc_12 + nwa_sc_12, 
                    data = vsg.ps, weights = weight,
                    subset = pk_hi_atabvMed == 0)
summary(m1.ps.vsg.lo)

# Affect on PID
m10.ps.vsg.lo <- lm(pid7_sc_16 ~ pid7_sc_12 + nwa_sc_12, 
                     data = vsg.ps, weights = weight,
                     subset = pk_hi_atabvMed == 0)
summary(m10.ps.vsg.lo)


### High PK
# PID on Affect
m1.ps.vsg.hi <- lm(nwa_sc_16 ~ pid7_sc_12 + nwa_sc_12, 
                    data = vsg.ps, weights = weight,
                    subset = pk_hi_atabvMed == 1)
summary(m1.ps.vsg.hi)

# Affect on PID
m10.ps.vsg.hi <- lm(pid7_sc_16 ~ pid7_sc_12 + nwa_sc_12, 
                     data = vsg.ps, weights = weight,
                     subset = pk_hi_atabvMed == 1)
summary(m10.ps.vsg.hi)


# Table -----
m1.ps.anes.lo_p <- m1.ps.anes.lo
m10.ps.anes.lo_p <- m10.ps.anes.lo
m1.ps.anes.hi_p <- m1.ps.anes.hi
m10.ps.anes.hi_p <- m10.ps.anes.hi
m1.ps.vsg.lo_p <- m1.ps.vsg.lo
m10.ps.vsg.lo_p <- m10.ps.vsg.lo
m1.ps.vsg.hi_p <- m1.ps.vsg.hi
m10.ps.vsg.hi_p <- m10.ps.vsg.hi

names(m1.ps.anes.lo_p$coefficients) <- names(coef(m1.ps.anes.lo))
names(m10.ps.anes.lo_p$coefficients) <- names(coef(m1.ps.anes.lo))
names(m1.ps.anes.hi_p$coefficients) <- names(coef(m1.ps.anes.lo))
names(m10.ps.anes.hi_p$coefficients) <- names(coef(m1.ps.anes.lo))
names(m1.ps.vsg.lo_p$coefficients) <- names(coef(m1.ps.anes.lo))
names(m10.ps.vsg.lo_p$coefficients) <- names(coef(m1.ps.anes.lo))
names(m1.ps.vsg.hi_p$coefficients) <- names(coef(m1.ps.anes.lo))
names(m10.ps.vsg.hi_p$coefficients) <- names(coef(m1.ps.anes.lo))


stargazer(m1.ps.anes.lo_p, m10.ps.anes.lo_p, 
          m1.ps.anes.hi_p, m10.ps.anes.hi_p,
          m1.ps.vsg.lo_p, m10.ps.vsg.lo_p, 
          m1.ps.vsg.hi_p, m10.ps.vsg.hi_p,
          title = "Political Awareness's Moderating Effect on the relationship between Whites' Partisanship and Affect Differential",
          covariate.labels = c("Partisanship$_{t-1}$",
                               "Affect Difference$_{t-1}$"),
          dep.var.labels = c("Affect Difference$_t$", "Partisanship$_t$",
                             "Affect Difference$_t$", "Partisanship$_t$"), 
          column.labels = c("1992-1994 ANES Low Awareness", "1992-1994 ANES High Awareness", 
                            "2012-2016 VOTER Survey Low Awareness", "2012-2016 VOTER Survey High Awareness"),
          column.separate = c(2,2),
          no.space = T, 
          notes = c("OLS regression results with robust standard errors in parentheses. Variables scaled 0-1. Analyses use population weights."), 
          notes.align = "l", intercept.bottom = T, 
          digits = 3, df = F, omit.stat = c("f", "adj.rsq"),
          align = T, star.char = c("*"), star.cutoffs = c(0.05), header = F
)
